package com.example.choupaiapp.main.activity.publish;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.bean.PreviewDataBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by ASUS on 2020/2/17.
 */

public class PublishPicturePreviewActivity extends CommonActivity {

    @BindView(R.id.complete)
    TextView complete;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    private int position;
    private ArrayList<String> list;
    private int index = 0;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_picture_preview);

    }

    PagerAdapter pagerAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return list != null ? list.size() : 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            (container).removeView((View) object);
        }

        public Object instantiateItem(ViewGroup container, int position) {
            View contentView = View.inflate(context, R.layout.item_photoview, null);
            PhotoView photo = contentView.findViewById(R.id.pv);
            String url = list.get(position);
            if (url != null) {
                if (url.endsWith(".gif")) {
                    Glide.with(context)
                            .load(url)
                            .asGif()
                            .thumbnail(0.1f)
                            .dontAnimate()
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.color.black)
                            .error(R.color.black)
                            .into(photo);
                } else {
                    Glide.with(context)
                            .load(url)
                            .thumbnail(0.1f)
                            .dontAnimate()
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.color.black)
                            .error(R.color.black)
                            .into(photo);
                }
                photo.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                    @Override
                    public void onPhotoTap(View view, float x, float y) {
                        finishActivity();
                    }

                });
            }
            (container).addView(contentView, 0);
            return contentView;
        }
    };

    @Override
    protected void initView() {

        Bundle bundle = getIntent().getExtras();
        list = bundle.getStringArrayList("path");
        position = bundle.getInt("position");
        index = position;
        viewpager.setAdapter(pagerAdapter);
        viewpager.setCurrentItem(position);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                index = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected void initData() {
    }

    //    @Override
    @OnClick({R.id.complete})
    public void onViewClicked(View view) {
//        super.onViewClicked(view);
        if (Utils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.complete:
                list.remove(index);
                PreviewDataBean cameraDataBean = new PreviewDataBean();
                cameraDataBean.setFlag("no_text");
                cameraDataBean.setIsVideo("no");
                cameraDataBean.setList(list);
                EventBus.getDefault().post(cameraDataBean);
                if (list.size() == 0) {
                    finishActivity();
                }
                pagerAdapter.notifyDataSetChanged();
                viewpager.setCurrentItem(index);


                break;
        }
    }

}
